package com.nhancv.kurentoandroid;

/**
 * author: Administrator
 * created on: 2022/1/4 17:46
 * description:
 */

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import android.app.Application;

import com.nhancv.kurentoandroid.App;
import com.nhancv.webrtcpeer.rtc_comm.ws.DefaultSocketService;
import com.nhancv.webrtcpeer.rtc_comm.ws.SocketCallBack;
import com.nhancv.webrtcpeer.rtc_comm.ws.SocketService;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

public class DefaultSocketService1 extends DefaultSocketService implements SocketService {
    private WebSocketClient client;
    private KeyStore keyStore;

    private SocketCallBack socketCallBack1;

    public DefaultSocketService1(Application application) {
        super(application);
    }

    @Override
    public void connect(String host) {
        this.connect(host, true);
    }

    @Override
    public void connect(String host, boolean force) {
        if (force) {
            this.close();
        } else if (this.isConnected()) {
            return;
        }

        URI uri;
        try {
            uri = new URI(host);
        } catch (URISyntaxException var8) {
            var8.printStackTrace();
            return;
        }

        this.client = new WebSocketClient(uri) {
            public void onOpen(ServerHandshake serverHandshake) {
                if (socketCallBack1 != null) {
                    socketCallBack1.onOpen(serverHandshake);
                }

            }

            public void onMessage(String s) {
                if (socketCallBack1 != null) {
                    socketCallBack1.onMessage(s);
                }

            }

            public void onClose(int i, String s, boolean b) {
                if (socketCallBack1 != null) {
                    socketCallBack1.onClose(i, s, b);
                }

            }

            public void onError(Exception e) {
                if (socketCallBack1 != null) {
                    socketCallBack1.onError(e);
                }

            }
        };

        try {
            String scheme = uri.getScheme();
            if (scheme.equals("https") || scheme.equals("wss")) {
//                this.setTrustedCertificate(App.sApplication.getAssets().open("server.crt"));
                String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
                tmf.init(this.keyStore);
                SSLContext sslContext = SSLContext.getInstance("TLS");
                sslContext.init((KeyManager[]) null, tmf.getTrustManagers(), (SecureRandom) null);
                this.client.setSocket(sslContext.getSocketFactory().createSocket());
                App.handleSSLHandshake();
            }
        } catch (Exception var9) {
            var9.printStackTrace();
        }

        this.client.connect();
    }

    @Override
    public void connect(String host, SocketCallBack socketCallBack) {
        this.connect(host, socketCallBack, true);
    }

    @Override
    public void connect(String host, SocketCallBack socketCallBack, boolean force) {
        this.setCallBack(socketCallBack);
        this.connect(host, force);
    }

    @Override
    public void sendMessage(String message) {
        super.sendMessage(message);
    }

    //    public void sendMessage(String message) {
//        this.executor1.execute(DefaultSocketService$$Lambda$1.lambdaFactory$(this, message));
//    }

    @Override
    public void setCallBack(SocketCallBack socketCallBack) {
        socketCallBack1 = socketCallBack;
    }

    @Override
    public void close() {
        if (this.isConnected()) {
            this.client.close();
        }

    }

    @Override
    public boolean isConnected() {
        return this.client != null && this.client.getConnection().isOpen();
    }

    @Override
    public void setTrustedCertificate(InputStream inputFile) {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
//            CertificateFactory cf = CertificateFactory.getInstance("X.509","BC");
            InputStream caInput = new BufferedInputStream(inputFile);
            Certificate ca = cf.generateCertificate(caInput);
            String keyStoreType = KeyStore.getDefaultType();
            this.keyStore = KeyStore.getInstance(keyStoreType);
            this.keyStore.load((InputStream) null, (char[]) null);
            this.keyStore.setCertificateEntry("ca", ca);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }
}